package user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import user.log.LogConstants;
import user.log.LogGenerate;
import user.service.IFeedBackService;
import user.service.IGainPassTemplateService;
import user.service.IInventoryService;
import user.service.IUserPassService;
import user.vo.FeedBack;
import user.vo.GainPassTemplateRequest;
import user.vo.Pass;
import user.vo.Response;

import javax.servlet.http.HttpServletRequest;

/**
 * passbook rest controller
 */

@RestController
@Slf4j
@RequestMapping("/passbook")
public class PassBookController {

    /**用户优惠券服务*/
    private final IUserPassService userPassService;

    /** 优惠券库存服务*/
    private final IInventoryService inventoryService;

    /** 领取优惠券服务 */
    private final IGainPassTemplateService gainPassTemplateService;

    /** 反馈服务*/
    private final IFeedBackService feedBackService;

    /** HttpServletRequest */
    private final HttpServletRequest httpServletRequest;

    @Autowired
    public PassBookController(IUserPassService userPassService,
                              IInventoryService inventoryService,
                              IGainPassTemplateService gainPassTemplateService,
                              IFeedBackService feedBackService,
                              HttpServletRequest httpServletRequest) {
        this.userPassService = userPassService;
        this.inventoryService = inventoryService;
        this.gainPassTemplateService = gainPassTemplateService;
        this.feedBackService = feedBackService;
        this.httpServletRequest = httpServletRequest;
    }

    /**
     *获取用户个人的优惠券信息
     * @param userId 用户id
     * @return {@link Response}
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/userpassinfo")
    Response userPassInfo(Long userId) throws Exception{

        LogGenerate.getLog(
                httpServletRequest,
                userId,
                LogConstants.AcitonName.USER_PASS_INFO,
                null
        );
        return userPassService.getUserPassInfo(userId);
    }

    /**
     * 获取用户使用了的优惠券信息
     * @param userId 用户id
     * @return {@link Response}
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/userusedpassinfo")
    Response userUsedPassInfo(Long userId) throws Exception{

        LogGenerate.getLog(
                httpServletRequest,
                userId,
                LogConstants.AcitonName.USER_USED_PASS_INFO,
                null
        );
        return userPassService.getUserUsedPassInfo(userId);
    }

    /**
     * 用户使用优惠券
     * @param pass {@link Pass}
     * @return {@link Response}
     */
    @ResponseBody
    @PostMapping("/userusepass")
    Response userUsePass(Pass pass){

        LogGenerate.getLog(
                httpServletRequest,
                pass.getUserId(),
                LogConstants.AcitonName.USER_USE_PASS,
                pass
        );
        return userPassService.userUsePass(pass);
    }

    /**
     * 获取库存信息
     * @param userId 用户id
     * @return {@link Response}
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/inventoryinfo")
    Response inventoryInfo(Long userId) throws Exception{

        LogGenerate.getLog(
                httpServletRequest,
                userId,
                LogConstants.AcitonName.INVENTORY__INFO,
                null
        );
        return inventoryService.getInventoryInfo(userId);
    }

    /**
     *用户领取优惠券
     * @param request {@link GainPassTemplateRequest}
     * @return {@link Response}
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/gainpasstemplate")
    Response gainPassTemplate(@RequestBody GainPassTemplateRequest request) throws Exception{

        LogGenerate.getLog(
                httpServletRequest,
                request.getUserId(),
                LogConstants.AcitonName.GAIN_PASS_TEMPLATE,
                request
        );
        return gainPassTemplateService.gainPassTemplate(request);
    }

    /**
     *用户创建评论
     * @param feedBack {@link FeedBack}
     * @return {@link Response}
     */
    Response CreateFeedBack(@RequestBody FeedBack feedBack){

        LogGenerate.getLog(
                httpServletRequest,
                feedBack.getUserId(),
                LogConstants.AcitonName.CREATE_FEEDBACK,
                feedBack
        );
        return feedBackService.createFeedBack(feedBack);
    }

    /**
     * 用户获取评论信息
     * @param userId 用户id
     * @return {@link Response}
     */
    @ResponseBody
    @GetMapping("/getfeedback")
    Response getFeedBack(Long userId){

        LogGenerate.getLog(
                httpServletRequest,
                userId,
                LogConstants.AcitonName.GET_FEEDBACK,
                null
        );
        return feedBackService.getFeedBack(userId);
    }

    /**
     * 异常演示接口
     * @return {@link Response}
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/exception")
    Response exception() throws Exception{
        throw new Exception("error !!!");
    }
}
